package scau.mis4.chanyulin.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.Reposit;
import scau.mis4.chanyulin.service.IRepositService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * Reposit Controller层
 */
@RestController
@RequestMapping("/reposit")
public class RepositController extends BaseController {

    @Autowired
    private IRepositService repositService;

    /**
     * 添加Reposit
     * @param reposit
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    public String addReposit(Reposit reposit) throws Exception {
        repositService.add(reposit);
        return createSuccessJSON();
    }

    /**
     * 删除某个Reposit
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    public String deleteRepositById(String id) throws Exception {
        repositService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 查找某个Reposit
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public String getRepositById(String id) throws Exception {
        Reposit reposit = repositService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", reposit);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部Reposit
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllReposit() throws Exception {
        List<Reposit> repositList = repositService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result",repositList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询Reposit
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listRepositByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Reposit> repositList = repositService.findAll();
        PageInfo<Reposit> pageInfo = new PageInfo<>(repositList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", repositList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个Reposit
     * @param reposit
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @RequiresRoles(value={"0","1", "3"},logical = Logical.OR)
    public String updateRepositById(Reposit reposit) throws Exception {
        repositService.updateById(reposit);
        return createSuccessJSON();
    }

}
